package club.menglinking.wechat.controller.wxindexController;

import club.menglinking.wechat.entity.Json;
import club.menglinking.wechat.entity.Menu;
import club.menglinking.wechat.restservice.Rest;
import club.menglinking.wechat.util.BaseUtil;
import club.menglinking.wechat.util.TokenContainer;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @User: Jrxsr
 * wxindexController
 * on 2019/1/4.
 **/
@RestController
public class wxindexController {

    @Value("${appid}")
    String appid;

    @Value("${appsecret}")
    String appsecret;

    @Autowired
    Rest restImpl;


    @GetMapping("createmenu")
    public String cm() throws JsonProcessingException {
        String accecc_token = TokenContainer.getTokenContainer().getToken().get("access_token")+"";
        Menu menu = new Menu();
        Menu.button button = new Menu().new button();
        Menu.sub_button sub_button = new Menu().new sub_button();
        sub_button.setName("投递信息");
        sub_button.setType("click");
        sub_button.setKey("投递信息");

        Menu.sub_button sub_button2 = new Menu().new sub_button();
        sub_button2.setName("面试信息");
        sub_button2.setType("view");
        sub_button2.setUrl("http://freenat.club:7500/");

        List<Menu.sub_button> buttons = new LinkedList<Menu.sub_button>();
        buttons.add(sub_button);
        buttons.add(sub_button2);
        button.setName("提交表单");
        button.setSub_button(buttons);

        Menu.button button2 = new Menu().new button();
        button2.setName("我要服务");
        button2.setType("view");
        button2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"http%3a%2f%2fhqj8xi.natappfree.cc&response_type=code&scope=snsapi_userinfo&state=success#wechat_redirect");

        List<Menu.button> buttonList = new LinkedList<Menu.button>();
        buttonList.add(button);
        buttonList.add(button2);

        menu.setButton(buttonList);
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(menu);
        System.out.println(str);
        str = restImpl.restpost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accecc_token, str, String.class)+"";
        System.out.println("cm方法返回："+str);
        return "success";
    }

    @PostMapping("getuserinfo")
    public String getinfo(@RequestParam Map<String,String> code) {
        System.out.println(code);
        String GETINFOURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appsecret+"&code="+code.get("code")+"&grant_type=authorization_code";
        String result = (String)restImpl.restget(GETINFOURL,String.class);
        Map map = JSON.parseObject(result);
        String DATA = null;
        if(!result.isEmpty()){
            String GETDATA = "https://api.weixin.qq.com/sns/userinfo?access_token="+map.get("access_token")+"&openid="+map.get("openid")+"&lang=zh_CN";
            DATA = (String)BaseUtil.getBaseUtil().getToken("GET",GETDATA);
            System.out.println(DATA);
        }
        return DATA;
    }



}